import { StackInfo } from "../../stack-list";
import { RuleInfo } from "../type";
import { ArrayCopy } from '../../array-utils';

export default class RuleIndexStack {

	private _indexs: number[] = [];

	private _lists: StackInfo<RuleInfo>[][] = [];

	public constructor(indexs: number[], lists: StackInfo<RuleInfo>[][]) {
		this._indexs = indexs;
		this._lists = lists;
	}

	public get size(): number {
		return this._indexs.length;
	}

	public get empty(): boolean {
		return this.size === 0;
	}

	public get index(): number {
		if (this.empty) { throw new Error("栈为空"); }
		return this._indexs[0];
	}
	public set index(value: number) {
		if (this.empty) { throw new Error("栈为空"); }
		this._indexs[0] = value;
	}

	public get select(): StackInfo<RuleInfo>[] {
		if (this.empty) { throw new Error("栈为空"); }
		return this._lists[0];
	}
	public set select(value: StackInfo<RuleInfo>[]) {
		if (this.empty) { throw new Error("栈为空"); }
		this._lists[0] = value;
	}

	public get info(): StackInfo<RuleInfo> | undefined {
		const length = this.select.length;
		const index = this.index;
		if (length === 0 || index >= length) { return undefined; }
		return this.select[index];
	}

	public get infoCheck(): StackInfo<RuleInfo> {
		const length = this.select.length;
		const index = this.index;
		if (length === 0 || index >= length) { throw new Error("选项为空"); }
		return this.select[index];
	}

	public last(): void {
		this.index--;
	}

	public next(): void {
		this.index++;
	}

	public push(index: number, list: StackInfo<RuleInfo>[]): void {
		this._indexs.unshift(index);
		this._lists.unshift(list);
	}

	public back(): void {
		if (this.empty) { throw new Error("突破栈底"); }
		this._indexs.shift();
		this._lists.shift();
	}

	public copy(): RuleIndexStack {
		return new RuleIndexStack(ArrayCopy(this._indexs), ArrayCopy(this._lists));
	}

}
